package com.rs.game.player;

import java.io.Serializable;

import com.rs.utils.Utils;


/**
 * 
 * @author TaylorMoon
 */
public class MoneyPouch implements Serializable {

	/**
	 * Serial ID
	 */
	private static final long serialVersionUID = -1699637125839716969L;

	/**
	 * The player using the pouch
	 */
	public Player player;

	/**
	 * Determines if the money pouch is toggled or isn't
	 */
	private static boolean isToggled;

	/**
	 * Initializes the class for the player logging in
	 * 
	 * @param x
	 *            - the player logging in
	 */
	public void init(Player x) {
		player = x;
		isToggled = false;
		refresh();
	}

	/**
	 * Removes a specific amount from the pouch
	 * 
	 * @param amount
	 *            - the amount to remove
	 */
	public void remove(int amount) {
		if (amount == 0)
			return;
		int index = (int) player.getMoneyInPouch();
		player.getPackets().sendRunScript(5561, 0, "n",
				(amount > index) ? index : (amount));
		player.setMoneyInPouch(amount > index ? 0 : index - amount);
		player.getInventory().addCoins(amount > index ? index : amount);
		refresh();
		print(true, (amount > index) ? (index) : (amount));
	}
	
	public void shopRemove(int amount) {
		if (amount == 0)
			return;
		int index = (int) player.getMoneyInPouch();
		player.getPackets().sendRunScript(5561, 0, "n",
				(amount > index) ? index : (amount));
		player.setMoneyInPouch(amount > index ? 0 : index - amount);
		refresh();
		print(true, (amount > index) ? (index) : (amount));
	}
	
	/**
	 * Adds a specific amount to the pouch
	 * 
	 * @param amount
	 *            - amount to add
	 */
	public void add(int amount) {
		int index = (int) player.getMoneyInPouch();
		if (index + amount >= Integer.MAX_VALUE || index + amount < 0) {
			player.getPackets().sendGameMessage(
					"You do not have enough room in your money pouch!");
			return;

		}
		player.getPackets().sendRunScript(5561, 1, amount);
		player.getInventory().deleteItem(995,
				amount == 0 ? player.getInventory().getNumerOf(995) : amount);
		player.addMoneyToPouch(amount);
		refresh();
		print(false, amount);
	}
	
	public void addShop(int amount) {
		int index = (int) player.getMoneyInPouch();
		if (index + amount >= Integer.MAX_VALUE || index + amount < 0) {
			player.getPackets().sendGameMessage(
					"You do not have enough room in your money pouch!");
			return;

		}
		player.getPackets().sendRunScript(5561, 1, amount);
		player.addMoneyToPouch(amount);
		refresh();
		print(false, amount);
	}

	/**
	 * Toggles the money pouch
	 */
	public void toggle() {
		player.getPackets().sendRunScript(5557, 1);
		refresh();
		isToggled = !isToggled;
	}

	/**
	 * Refreshes the money pouch
	 */
	public void refresh() {
		int index = (int) player.getMoneyInPouch();
		player.getPackets().sendRunScript(5560, index < 1 ? 0 : index, "n");

	}

	/**
	 * A method that checks if the pouch contains money inside it
	 * 
	 * @return - true if it does, false otherwise
	 */
	public boolean contains(int i) {
		return (player.getMoneyInPouch() >= i);
	}

	/**
	 * Adds the whole pouch value to the players' bank
	 */
	public void addToBank() {
		int index = (int) player.getMoneyInPouch();
		if (!((player.getBank().getItem(995).getAmount() + index) < Integer.MAX_VALUE || ((player
				.getBank().getItem(995).getAmount() + index) < 1))) {
			player.getPackets().sendGameMessage(
					"You don't have enough bank space!");
			return;
		} else {
			player.getBank().addItem(995, index, true);
			player.getPackets().sendRunScript(5561, 0, "n", index);
			player.setMoneyInPouch(0);
		}
	}

	/**
	 * Sends the added money/removed money to the player removing/adding the
	 * money
	 * 
	 * @param remove
	 *            - sends the removing message if true
	 * @param amount
	 *            - amount being removed
	 */
	public void print(boolean remove, int amount) {
		player.getPackets()
				.sendGameMessage(
						remove ? (amount == 1 ? "One coin has been removed from your money pouch."
								: Utils.formatTypicalInteger(amount)
										+ " coins have been removed from your money pouch.")
								: (amount == 1 ? "One coin has been added to your money pouch."
										: Utils.formatTypicalInteger(amount)
												+ " coins have been added to your money pouch."));
	}

	/**
	 * Jeremy's gay
	 * 
	 * @param coinAmount
	 *            amount of coins
	 * @param removedAmount
	 *            amount to remove
	 * @param trade
	 *            if you're trading or not
	 * @param shop
	 *            if it's a shop
	 */
	public void removeMoney(int coinAmount, int removedAmount, boolean trade,
			boolean shop, int amount) {
		if (shop) {
			if (coinAmount > player.getMoneyInPouch()) {
				player.removeMoneyFromPouch((int) player.getMoneyInPouch());
				return;
			} else {
				player.removeMoneyFromPouch(coinAmount);
			}
		} else if (trade) {
			player.removeMoneyFromPouch(coinAmount);
		} else {
			if (coinAmount > player.getMoneyInPouch()) {
				player.getPackets().sendGameMessage(
						"You don't have that many Coins in your Money Pouch.");
				return;
			}
			player.removeMoneyFromPouch(coinAmount);
			player.getInventory().addItem(995, removedAmount);
		}
		print(true, removedAmount);
		player.getPackets().sendRunScript(5561, 0, removedAmount);
		refresh();
	}

}